package com.example.fangqidian.adapter;

import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * 创建日期:2019/1/15 on 9:48
 * 描述：自定义万能RecyclerView的Adapter
 * 作者: zhougan
 */
public abstract class BaseRecycleViewAdapter<T> extends RecyclerView.Adapter<BaseRecycleViewAdapter.VH>{
    private List<T> mData;//数据
    //item点击事件接口
    private OnItemListener mOnItemListener;

    public BaseRecycleViewAdapter() {

    }

    public BaseRecycleViewAdapter(List<T> data) {
        this.mData = data;
    }

    public List<T> getData() {
        return mData;
    }

    public void setData(List<T> data) {
        this.mData = data;
    }

    public T getItemData(int position){
        return mData.get(position);
    }

    public void setOnItemListener(OnItemListener onItemListener){
        this.mOnItemListener=onItemListener;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new VH(LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false),mOnItemListener);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        onBindData(holder,getItemData(position),position,getItemViewType(position));
    }



    @Override
    public int getItemCount() {
        if(mData!=null){
            return mData.size();
        }
        return 0;
    }

    /**
     * 返回布局文件的id
     * @param viewType
     * @return
     */
    public abstract int getLayoutId(int viewType);

    /**
     * 绑定数据到item
     * @param holder
     * @param data
     * @param position
     * @param viewType
     */
    public abstract void onBindData(VH holder,T data,int position,int viewType);




    //自定义ViewHolder
    public static class VH extends RecyclerView.ViewHolder{
        private SparseArray<View> mViews;//存储布局中的字组件item.findViewById集合
        private OnItemListener mOnItemListener;

        private VH(View itemView,OnItemListener onItemListener) {
            super(itemView);
            mViews=new SparseArray<>();
            this.mOnItemListener=onItemListener;
            if(mOnItemListener!=null){
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //getLayoutPosition是RecyclerView定义的获取item位子的方法
                        mOnItemListener.onItemClickListener(v,getLayoutPosition());
                    }
                });
                itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        return mOnItemListener.onItemLongClickListener(v,getLayoutPosition());
                    }
                });
            }
        }

        /**
         * 根据id获取布局文件中的子组件
         * @param id
         * @param <T>
         * @return
         */
        public <T extends View> T getView(@IdRes int id){
            View view=mViews.get(id);
            if(view==null){
                 view=itemView.findViewById(id);
                 mViews.put(id,view);
            }
            return (T)view;
        }

        /**
         * 设置文字比较常用,所以单独定义出来
         * @param id
         * @param value
         */
        public void setText(int id,String value){
            TextView textView=getView(id);
            textView.setText(value);
        }
    }

    /**
     * item点击接口
     */
    public static interface OnItemListener{
        void onItemClickListener(View view, int position);
        boolean onItemLongClickListener(View view, int position);
    }
}
